package com.elong.businesstravel.modules.home.hotel.hotelpayment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.Bank;
import com.elong.businesstravel.modules.home.adapter.SimpleListAdapter;
import com.elong.businesstravel.protocol.order.GetBankListRequest;
import com.elong.businesstravel.protocol.order.GetBankListResponse;

/**
 * 发卡银行列表
 * 
 * @author lenovo
 * 
 */
public class BankListActivity extends BaseActivity {

	private List<Bank> mBanks;

	private ListView mListView;
	private SimpleListAdapter mSimpleAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bank_list);

		init();
	}

	@Override
	protected void init() {

		mListView = (ListView) findViewById(R.id.listView);

		setTitleNavBar();

		setListener();

		getBankList();
	}

	@Override
	protected void setListener() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(null != mBanks){
					Intent intent = new Intent();
					intent.putExtra("bank", mBanks.get(arg2));
					setResult(RESULT_OK, intent);
					quit();
				}
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("发卡银行");
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				quit();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void getBankList() {
		NetworkClient.getNetworkClient().PostRequest(
				new GetBankListRequest(mContext), new GetBankListResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mBanks = banks;
								List<String> strings = new ArrayList<String>();
								for (int i = 0; i < mBanks.size(); i++) {
									strings.add(mBanks.get(i).toString());
								}
								mSimpleAdapter = new SimpleListAdapter(
										mContext, strings, "");
								mListView.setAdapter(mSimpleAdapter);
							}
						});
					}
				});
	}
	
	private void quit() {
		finish();
		overridePendingTransition(R.anim.activity_alpha_fixed, R.anim.activity_top_bottom_exit_anim_translate);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
